package de.java.shortys.myhome.api.view;

import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGAPPLICATIONVIEW_BACKGROUND;
import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGAPPLICATIONVIEW_SIDEBAR_WIDTH;
import static de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager.getInstance;
import static de.java.shortys.utils.view.ViewLocationUtillityConstants.LOCATION_CENTER;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import net.miginfocom.swing.MigLayout;
import de.java.shortys.utils.view.ViewUtils;

public class SwingApplicationView implements IView {

	public SwingApplicationView() {
		build();
	}

	private JFrame frame;
	private JPanel component;
	private JPanel sidebar;
	private JPanel contentPane;
	private LayoutManager sidebarLayoutManager;
	private JToolBar toolbar;

	public void add(JButton button) {
		toolbar.add(button);
		toolbar.revalidate();
		toolbar.repaint();
	}

	public void activate(SwingModuleView view) {
		sidebar.add(view.build(), "dock north");
		sidebar.revalidate();
		sidebar.repaint();
	}

	public void deactivate(SwingModuleView view) {
		sidebar.remove(view.build());
		sidebar.revalidate();
		sidebar.repaint();
	}

	public void activate(IView view) {
		contentPane.removeAll();
		contentPane.add(view.build());
		contentPane.revalidate();
		contentPane.repaint();
	}

	public void deactivate(IView view) {
		contentPane.remove(view.build());
		contentPane.revalidate();
		contentPane.repaint();
	}

	@Override
	public Component build() {
		if (frame == null) {
			createFrame();
			createSidebar();
			createContentPane();
			createToolbar();
		}
		return frame;
	}

	private void createContentPane() {
		contentPane = new JPanel();
		contentPane.setOpaque(false);
		contentPane.setLayout(new BorderLayout());
		component.add(contentPane);
	}

	private void createToolbar() {
		toolbar = new JToolBar();
		toolbar.setFloatable(false);
		toolbar.setBorder(null);
		toolbar.setFocusable(false);
		toolbar.setBackground(getInstance().getColor(SWINGAPPLICATIONVIEW_BACKGROUND));
		toolbar.setBorderPainted(false);
		frame.add(BorderLayout.NORTH, toolbar);
	}

	private void createSidebar() {
		sidebarLayoutManager = new MigLayout("", "[grow]", "");
		sidebar = new JPanel(sidebarLayoutManager);
		sidebar.setPreferredSize(new Dimension(getInstance().getInteger(SWINGAPPLICATIONVIEW_SIDEBAR_WIDTH), 0));
		sidebar.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		sidebar.setOpaque(false);
		component.add(BorderLayout.WEST, sidebar);
	}

	private void createFrame() {
		frame = new JFrame();
		frame.setSize(800, 600);

		component = new JPanel();
		component.setLayout(new BorderLayout());
		component.setBackground(getInstance().getColor(SWINGAPPLICATIONVIEW_BACKGROUND));
		frame.add(component);
		ViewUtils.setLocation(LOCATION_CENTER, frame);
	}

	@Override
	public void update(Observable o, Object arg) {
		// Empty
	}

	@Override
	public void clear() {
		// Empty
	}

}
